<html>
<head><meta charset="utf-8"><title>Give a compile error when not on windows? · t-cargo · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/index.html">t-cargo</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html">Give a compile error when not on windows?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214991283"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214991283" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214991283">(Oct 29 2020 at 16:20)</a>:</h4>
<p>I want to</p>
<ol>
<li>Give a compile error when not on windows, and</li>
<li>Give no other errors, and</li>
<li>Have the package work normally on Windows</li>
</ol>
<p>The first thing I tried was <code>#[cfg(not(windows))]</code> compile error in the source code. But that will also give all other errors that show up (things like 'could not find <code>windows</code> in <code>os</code>').</p>
<p>Then I tried <code>#![cfg(windows)]</code> on the whole crate, and additionally have the <code>compile_error!</code>. But that completely ignored the compile error, since it's part of the crate.</p>
<p>My last idea was to use a different path for the <code>lib.rs</code> when not on windows, and have the only thing in the file be a compile error. But I don't see a way to do that in cargo. Is there a syntax like this?</p>
<div class="codehilite" data-code-language="TOML"><pre><span></span><code><span class="k">[package.'cfg(not(windows))']</span>
<span class="n">path</span> <span class="o">=</span> <span class="s">"src/compile_error.rs"</span>
</code></pre></div>

<p>If not, is there a simpler way to do this?</p>



<a name="214991616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214991616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214991616">(Oct 29 2020 at 16:23)</a>:</h4>
<p>You can use a build script which will error out on other platforms.</p>



<a name="214991687"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214991687" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214991687">(Oct 29 2020 at 16:24)</a>:</h4>
<p>ah right - I can find the target with <code>std::env::var("TARGET")</code>?</p>



<a name="214991761"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214991761" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214991761">(Oct 29 2020 at 16:24)</a>:</h4>
<p>hmm that doesn't tell me whether it's windows though - there are many windows targets</p>



<a name="214991782"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214991782" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214991782">(Oct 29 2020 at 16:24)</a>:</h4>
<p>I want <code>cfg(windows)</code>, which includes all of them</p>



<a name="214991845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214991845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214991845">(Oct 29 2020 at 16:25)</a>:</h4>
<p><code>CARGO_CFG_WINDOWS</code> looks right</p>



<a name="214991854"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214991854" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eric Huss <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214991854">(Oct 29 2020 at 16:25)</a>:</h4>
<p>There are separate <code>CFG</code> env vars for all the normal target stuff, like <code>CARGO_CFG_WINDOWS</code> <a href="https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts">docs</a></p>



<a name="214992488"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/214992488" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#214992488">(Oct 29 2020 at 16:29)</a>:</h4>
<p>that worked great, thank you!</p>



<a name="215002961"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/215002961" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#215002961">(Oct 29 2020 at 17:44)</a>:</h4>
<p>What would it take for Cargo to have a <em>declarative</em> way to specify this inside <code>Cargo.toml</code>, so that Cargo can detect that a crate doesn't support the current platform and reject it with a cleaner error message without having to compile it (or ideally download it)?</p>



<a name="215002997"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/215002997" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#215002997">(Oct 29 2020 at 17:44)</a>:</h4>
<p>I have some crates in a similar position.</p>



<a name="215003320"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/215003320" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#215003320">(Oct 29 2020 at 17:47)</a>:</h4>
<p>personally I'm ok with it taking a little longer, since I wouldn't expect people to run into this error more than once</p>



<a name="215003359"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/215003359" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Eh2406 <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#215003359">(Oct 29 2020 at 17:47)</a>:</h4>
<p>My thouts <a href="https://github.com/rust-lang/cargo/issues/8723#issuecomment-696314612">https://github.com/rust-lang/cargo/issues/8723#issuecomment-696314612</a></p>



<a name="215003384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/215003384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#215003384">(Oct 29 2020 at 17:47)</a>:</h4>
<p>I've been thinking of making a wiki with "rust tips and tricks" that I think would work well here</p>



<a name="215005331"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/215005331" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#215005331">(Oct 29 2020 at 18:00)</a>:</h4>
<p>Winapi simply uses <code>#![cfg(windows)]</code> thereby compiling to nothing on non-windows systems.</p>



<a name="215014203"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/246057-t-cargo/topic/Give%20a%20compile%20error%20when%20not%20on%20windows%3F/near/215014203" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F.html#215014203">(Oct 29 2020 at 19:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/246057-t-cargo/topic/Give.20a.20compile.20error.20when.20not.20on.20windows.3F/near/215005331">said</a>:</p>
<blockquote>
<p>Winapi simply uses <code>#![cfg(windows)]</code> thereby compiling to nothing on non-windows systems.</p>
</blockquote>
<p>then giving bad error messages if you try to use it, yes. Winapi is actually the use case for this. <a href="https://github.com/retep998/winapi-rs/pull/949">https://github.com/retep998/winapi-rs/pull/949</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>